|
Download
FAQ History |
|
API
Search Feedback |
An Application Example that Consumes Messages from a Remote J2EE Server
This section and the following section both explain how to write, compile, package, deploy, and run a pair of J2EE applications that use the JMS API and run on two J2EE servers. A common practice is to deploy different components of an enterprise application on different systems within a company, and these examples illustrate on a small scale how to do this for an application that uses the JMS API.
However, the two examples work in slightly different ways. In this first example, the deployment information for a message-driven bean specifies the remote server from which it will consume messages. In the next example, the same bean is deployed on two different servers, so it is the client application that specifies the servers (one local, one remote) to which it is sending messages.
This first example divides the example in A Simple J2EE Application that Uses the JMS API into two applications, one containing the application client and the other containing the message-driven bean.
The section covers the following topics:
You will find the source files for this section in
<INSTALL>/j2eetutorial14/examples/jms/consumeremote/. Path names in this section are relative to this directory.Overview of the Applications
Except for the fact that it is packaged as two separate applications, this example is almost identical to the one in A Simple J2EE Application that Uses the JMS API:
The basic steps of the applications are as follows.
- The administrator starts two J2EE servers, one on each system.
- On one server, the administrator deploys the client application, which sends three messages to a queue.
- On the other server, the administrator deploys the message-driven bean application, which specifies the URL of the server where the client is deployed.
- The message-driven bean consumes the messages.
Figure 21-4 illustrates the structure of this application. You can see that it is almost identical to Figure 21-1 except that there are two J2EE servers. The queue is not associated with either server; it must exist on both servers.
![]()
Figure 21-4 A J2EE Application that Consumes Messages from a Remote Server
Writing the Application Components
Writing the components of the applications involves
The application client,
earthclient/src/SimpleClient.java, is identical to the one in Coding the Application Client: SimpleClient.java.Similarly, the message-driven bean,
marsmdb/src/MessageBean.java,is identical to the one in Coding the Message-Driven Bean: MessageBean.java.The only major difference is that the client and bean are packaged in two separate applications.
Creating and Packaging the Applications
This example uses the connection factory named
jms/QueueConnectionFactoryand the queue namedjms/Queue, both of which you created in Writing Simple JMS Client Applications.We'll suppose, as we did in Running JMS Client Programs on Multiple Systems, that the two servers are named
earthandmars.Creating and packaging this application involve several steps:
Starting the J2EE Application Server and Compiling the Source Files
Which system you use to package and deploy the application and which system you use to run the client depends on your network configuration. If both systems are visible to each other, it does not matter which system you use for which tasks. If only one system is visible to the other, do the packaging and deployment on the system from which the other system is visible, and run the client on the other system. These instructions assume that
earthis visible frommars, butmarsis not visible fromearth.
- Start the J2EE Application Server on
earth, if it is not already running.- Start the J2EE Application Server on
mars, if it is not already running.- Also on
mars, go to the directoryearthclient. Compile the source files:
asant build- Also on
mars, go to the directorymarsmdb. Compile the source files:
asant buildStarting the J2EE Deploytool and Creating the Applications
On
mars, do the following to create the application for the client:On
mars, do the following to create the application for the message-driven bean:Packaging the Application Client
Perform the following steps to package the application client:
- Choose File
New
Application Client JAR to start the Application Client Wizard.
- In the JAR File Contents screen:
- Verify that Create New AppClient Module in Application is selected and that the application is
EarthClientApp.- In the AppClient Display Name field, type
SimpleClient.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
earthclient/build/directory. SelectSimpleClient.classfrom the Available Files tree area and click Add, then OK.- In the General screen, select
SimpleClientin the Main Class combo box.- In the Message Destination References screen, click Add. In the dialog box:
- In the Resource References screen:
- Click Finish.
- Click the Message Destinations tab. In the inspector pane:
Packaging the Message-Driven Bean
Perform the following steps to package the message-driven bean:
- Choose File
New
Enterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
- In the EJB JAR screen:
- Select Create New JAR Module in Application and verify that the application is
MarsMDBApp.- In the JAR Display Name field, type
MDBJAR.- Click the Edit button next to the Contents text area.
- In the dialog box, locate the
marsmdb/build/directory. SelectMessageBean.classfrom the Available Files tree area and click Add, then OK.- In the General screen:
- In the Configuration Options screen, check the Transaction Management box.
- In the Message-Driven Bean Settings screen:
- Click Add in the Activation Configuration Properties area.
- Type
destinationin the Property Name field andPhysicalQueuein the Value field.- Click Add again.
- Type
destinationTypein the Property Name field andjavax.jms.Queuein the Value field.- Click Add again.
- Type
addressListin the Property Name field and the name of the remote system in the Value field. This means that the bean will consume messages from the remote system.- Choose
javax.jms.Queuefrom the Destination Type menu.- Type
PhysicalQueuein the Destination Name field.- Click Deployment Settings. In the dialog box, type
jmsrain the Resource Adapter field and click OK.- In the Transaction Management screen, choose Container-Managed.
- Click Finish.
- Click the Message Destinations tab. In the inspector pane:
Verifying and Updating the JNDI Names
In the J2EE Application Server, the JNDI name for a message-driven bean is the destination it receives messages from, not the bean name. You need to update the JNDI name for the
MarsMDBAppapplication and verify it for theEarthClientAppapplication.The JNDI name for the application should appear as shown in Table 21-8. Only the Application Pane has any content.
Table 21-8 Application Pane for MarsMDBApp Component Type Component JNDI Name EJB MessageBean jms/Queue
Select the
EarthClientAppapplication and click the JNDI Names tab.The JNDI name for the application should appear as shown in Table 21-9. Only the References Pane has any content.
Table 21-9 References Pane for EarthClientApp Ref. Type Referenced By Reference Name JNDI Name Resource SimpleClient jms/MyConnectionFactory jms/QueueConnectionFactory
Deploying the Applications
Before you can deploy the applications, you must add the remote server. Perform the following steps:
To deploy the
EarthClientAppapplication, perform the following steps:
- Choose File
Save to save the application.
- Choose Tools
Deploy.
- In the dialog box, select the URI for the remote system:
deployer:Sun:S1AS::system-name:4848- Type your administrative user name and password (if they are not already filled in) and click OK.
- In the Distribute Module dialog box, click Close when the process completes.
To deploy the
MarsMDBAppapplication, perform the following steps:Saving the Client JAR and Running the Application Client
Perform the following steps:
- In the deploytool, select the server node.
- Select
EarthClientAppfrom the list of deployed objects, then click Client Jar.- Choose Browse to navigate to the directory on the remote system from which you will run the client. When you reach the directory, click Select, then click OK. Click OK in the information dialog. You will find a file named
EarthClientAppClient.jarin the specified directory.To run the client,
On
earth, the output of theappclientcommand looks something like this:INFO: Binding name:`java:comp/env/jms/QueueName` Sending message: This is message 1 Sending message: This is message 2 Sending message: This is message 3On
mars, the output in the server log looks like this:In MessageBean.MessageBean() In MessageBean.setMessageDrivenContext() In MessageBean.ejbCreate() MESSAGE BEAN: Message received: This is message 1 MESSAGE BEAN: Message received: This is message 2 MESSAGE BEAN: Message received: This is message 3Undeploying and Removing the Applications
To undeploy and remove the applications, perform the following steps:
If you wish, you can manually delete the EAR and client JAR files.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.